package top.majuan.study.study.dao;

import com.mysql.cj.x.protobuf.MysqlxCrud;
import top.majuan.study.study.entity.TbCpuInfo;
import top.majuan.study.study.entity.TbDept;
import top.majuan.study.study.entity.TbEmployee;
import top.majuan.study.study.entity.TbNews;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class TbCpuInfoDAO {
    private static final String INSERT ="insert into tb_cpu_info(cpu,info,cores,threads,frequency,max_frequency,post_time) values(?,?,?,?,?,?,?)";
    public static final String QUERY = "select * from tb_cpu_info";
    public static final String QUERY_BY_ID = "select id,cpu,info,cores,threads,frequency,max_frequency,post_time from tb_cpu_info where id = ?";
    public static final String UPDATE = "update tb_cpu_info set cpu=?,info=?,cores=?,threads=?,frequency=?,max_frequency=?,post_time=? where id=?";
    public static final String DELETE = "delete from tb_cpu_info where id=?";
    public static int insert(TbCpuInfo info) throws Exception{
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(INSERT);
        ps.setString(1, info.getCpu());
        ps.setString(2, info.getInfo());
        ps.setInt(3, info.getCores());
        ps.setInt(4, info.getThreads());
        ps.setString(5, info.getFrequency());
        ps.setString(6, info.getMax_frequency());
        ps.setTimestamp(7,new Timestamp(info.getPost_time().getTime()));
        int i = ps.executeUpdate();
        conn.close();
        return i;
    }
    public static List<TbCpuInfo> query() throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(QUERY);
        List<TbCpuInfo> list = new ArrayList<>();
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            TbCpuInfo tbCpuInfo = new TbCpuInfo();
            tbCpuInfo.setId(rs.getInt("id"));
            tbCpuInfo.setCpu(rs.getString("cpu"));
            tbCpuInfo.setInfo(rs.getString("info"));
            tbCpuInfo.setCores(rs.getInt("cores"));
            tbCpuInfo.setThreads(rs.getInt("threads"));
            tbCpuInfo.setFrequency(rs.getString("frequency"));
            tbCpuInfo.setMax_frequency(rs.getString("max_frequency"));
            tbCpuInfo.setPost_time(rs.getTimestamp("post_time"));
            list.add(tbCpuInfo);
        }
        conn.close();
        return list;
    }
    public static TbCpuInfo queryById(Integer id) throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(QUERY_BY_ID);
        ps.setInt(1, id);
        ResultSet rs = ps.executeQuery();
        TbCpuInfo cpuInfo = null;
        if (rs.next()) {
            cpuInfo = new TbCpuInfo();
            cpuInfo.setId(rs.getInt("id"));
            cpuInfo.setCpu(rs.getString("cpu"));
            cpuInfo.setInfo(rs.getString("info"));
            cpuInfo.setCores(rs.getInt("cores"));
            cpuInfo.setThreads(rs.getInt("threads"));
            cpuInfo.setFrequency(rs.getString("frequency"));
            cpuInfo.setMax_frequency(rs.getString("max_frequency"));
            cpuInfo.setPost_time(rs.getTimestamp("post_time"));
        }
        conn.close();
        return cpuInfo;
    }
    public static int update(TbCpuInfo cpuInfo) throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(UPDATE);
        ps.setString(1, cpuInfo.getCpu());
        ps.setString(2, cpuInfo.getInfo());
        ps.setInt(3, cpuInfo.getCores());
        ps.setInt(4, cpuInfo.getThreads());
        ps.setString(5, cpuInfo.getFrequency());
        ps.setString(6, cpuInfo.getMax_frequency());
        ps.setTimestamp(7,new Timestamp(cpuInfo.getPost_time().getTime()));
        ps.setInt(8, cpuInfo.getId());
        int i = ps.executeUpdate();
        conn.close();
        return i;
    }
    public static int delete(Integer id) throws Exception {
        Connection conn = DBHelper.getConnection();
        PreparedStatement ps = conn.prepareStatement(DELETE);
        ps.setInt(1, id);
        int i = ps.executeUpdate();
        conn.close();
        return i;
    }

}

